package com.ruoyi.quartz.task;

import com.ruoyi.system.domain.HotspotDataRealtime;
import com.ruoyi.system.domain.HotspotPlatform;
import com.ruoyi.system.service.IHotspotDataRealtimeService;
import com.ruoyi.system.service.IHotspotPlatformService;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * class
 * 获取热点数据爬虫
 *
 * @description: 获取热点数据爬虫
 * @author: mabo
 * @date: 2022/1/19
 */
@Component("getHotspotTask")
public class GetHotspotTask {

    private static Logger log = LoggerFactory.getLogger(GetHotspotTask.class);

    @Autowired
    private IHotspotPlatformService iHotspotPlatformService;

    @Autowired
    private IHotspotDataRealtimeService iHotspotDataRealtimeService;

    public static Document getWebPageContext() throws IOException {
        Document document = Jsoup.connect("https://tophub.today/").get();
        return document;
    }

    //@Scheduled(cron = "*/10 * * * * ?")
    public void getHotspotDataTask() {
        System.out.println("定时任务开始执行！");
        try {
            HotspotPlatform hotspotPlatform = new HotspotPlatform();
            //获取热点平台名称和code
            List<HotspotPlatform> lists = iHotspotPlatformService.selectHotspotPlatformList(hotspotPlatform);
            for (HotspotPlatform hotspotPlatform1 : lists) {
                HotspotDataRealtime hotspotDataRealtime = new HotspotDataRealtime();
                String code = hotspotPlatform1.getPlatformCode();
                String platformId = hotspotPlatform1.getPlatformId();
                //抓取网页数据开始处理
                Element element = getWebPageContext().getElementById(code);
                String[] classDataTitle = element.getElementsByClass("t").toString().replaceAll("<span class=\"t\">", "").replaceAll("</span>", "").replaceAll("\n", "|").split("\\|");
                String[] classDataDegree = element.getElementsByClass("e").toString().replaceAll("<span class=\"e\">", "").replaceAll("</span>", "").replaceAll("\n", "|").split("\\|");
                List linkDataList = new ArrayList();
                //链接使用Matcher和Pattern进行正则处理
                Matcher matcher = Pattern.compile("<a href=\"(https.*?)\"").matcher(element.toString());
                while (matcher.find()) {
                    linkDataList.add(matcher.group());
                }
                //循环存数据
                for (int i = 0; i < classDataTitle.length; i++) {
                    hotspotDataRealtime.setPlatformId(platformId);
                    hotspotDataRealtime.setHotspotContent(classDataTitle[i]);
                    hotspotDataRealtime.setHotspotDegree(classDataDegree[i]);
                    hotspotDataRealtime.setHotspotLink(String.valueOf(linkDataList.get(i)).replaceAll("<a href=\"","").replaceAll("\"",""));
                    iHotspotDataRealtimeService.insertHotspotDataRealtime(hotspotDataRealtime);
                }
            }
        } catch (IOException e) {
            log.error("处理实时热点数据失败！", e);
        }
    }

}
